package com.example.demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.weaver.EnumAnnotationValue;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Component
@Aspect
@Slf4j
public class LogAspect {

    @Pointcut("execution(public  * com.example.demo.controller.*.*(..))")
    public void weblog(){
    }



    @Before("weblog()")
    public void doBefor(JoinPoint joinPoint) throws Throwable{
        //接收到请求
        ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        log.info("url:"+request.getRequestURI().toString());
        log.info("http_method:"+request.getMethod());
        log.info("ip:"+request.getRemoteAddr());
        Enumeration<String> enu=request.getParameterNames();
        while (enu.hasMoreElements()){
            String name=(String) enu.nextElement();
            log.info("name:{},value:{}",name,request.getParameter(name));
        }
    }



    @AfterReturning(returning = "ret",pointcut = "weblog()")
    public void afterRetruning(Object ret) throws Throwable{
        log.info("返回结果："+ret.toString());
    }
}
